function varargout = FeatureLearning(varargin)
% FEATURELEARNING MATLAB code for FeatureLearning.fig
%      FEATURELEARNING, by itself, creates a new FEATURELEARNING or raises the existing
%      singleton*.
%
%      H = FEATURELEARNING returns the handle to a new FEATURELEARNING or the handle to
%      the existing singleton*.
%
%      FEATURELEARNING('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in FEATURELEARNING.M with the given input arguments.
%
%      FEATURELEARNING('Property','Value',...) creates a new FEATURELEARNING or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before FeatureLearning_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to FeatureLearning_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help FeatureLearning

% Last Modified by GUIDE v2.5 23-Jun-2015 14:09:31

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @FeatureLearning_OpeningFcn, ...
                   'gui_OutputFcn',  @FeatureLearning_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before FeatureLearning is made visible.
function FeatureLearning_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to FeatureLearning (see VARARGIN)

% Choose default command line output for FeatureLearning
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);
setappdata(handles.output,'RSdata',varargin{1});
setappdata(handles.output,'FileName',varargin{2});
disp(getappdata(handles.output,'FileName'));
% UIWAIT makes FeatureLearning wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = FeatureLearning_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in DBNtrainbutton.
function DBNtrainbutton_Callback(hObject, eventdata, handles)
% hObject    handle to DBNtrainbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
imgage = double( getappdata(handles.output,'RSdata') );
mina = min(imgage(:));
maxa = max(imgage(:));
imgage = (imgage-mina)./(maxa-mina);

rf = str2num(get(handles.dbntrainedit,'String')); 

params.numTrain = str2num(get(handles.numbertrainedit,'String')); 
params.numLayerNodes  = [100 100 50]; 
params.numIters = str2num(get(handles.itertrainedit,'String'));
params.momentum = 0;  
params.alpha = 1; 

set(handles.statustext,'String','Start Training DBN! Please Wait!');

[img,dbn] = fvLearnDBN(imgage,rf,params);
assignin('base',['DBNFeatures4' getappdata(handles.output,'FileName')],img); 
assignin('base',['DBNmodel4' getappdata(handles.output,'FileName')],dbn); 
set(handles.statustext,'String','Finish DBN!');

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



function dbntrainedit_Callback(hObject, eventdata, handles)
% hObject    handle to dbntrainedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of dbntrainedit as text
%        str2double(get(hObject,'String')) returns contents of dbntrainedit as a double


% --- Executes during object creation, after setting all properties.
function dbntrainedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dbntrainedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function numbertrainedit_Callback(hObject, eventdata, handles)
% hObject    handle to numbertrainedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of numbertrainedit as text
%        str2double(get(hObject,'String')) returns contents of numbertrainedit as a double


% --- Executes during object creation, after setting all properties.
function numbertrainedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to numbertrainedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function itertrainedit_Callback(hObject, eventdata, handles)
% hObject    handle to itertrainedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of itertrainedit as text
%        str2double(get(hObject,'String')) returns contents of itertrainedit as a double


% --- Executes during object creation, after setting all properties.
function itertrainedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to itertrainedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in opendbnbutton.
function opendbnbutton_Callback(hObject, eventdata, handles)
% hObject    handle to opendbnbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



function filenameedit_Callback(hObject, eventdata, handles)
% hObject    handle to filenameedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of filenameedit as text
%        str2double(get(hObject,'String')) returns contents of filenameedit as a double


% --- Executes during object creation, after setting all properties.
function filenameedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to filenameedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function sizelearnedit_Callback(hObject, eventdata, handles)
% hObject    handle to sizelearnedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of sizelearnedit as text
%        str2double(get(hObject,'String')) returns contents of sizelearnedit as a double


% --- Executes during object creation, after setting all properties.
function sizelearnedit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sizelearnedit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in SAEtrainbutton.
function SAEtrainbutton_Callback(hObject, eventdata, handles)
% hObject    handle to SAEtrainbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
img = double( getappdata(handles.output,'RSdata') );
mina = min(img(:));
maxa = max(img(:));
img = (img-mina)./(maxa-mina);
rf = str2num(get(handles.dbntrainedit,'String')); 
params.numTrain = str2num(get(handles.numbertrainedit,'String')); 
params.numLayerNodes  = [100 100 50]; 
params.numIters = str2num(get(handles.itertrainedit,'String'));
params.momentum = 0;  
params.alpha = 1; 
set(handles.statustext,'String','Start Training SAE! Please Wait!');
[img,sae] = fvLearnSAE(img,rf,params);
assignin('base',['SAEFeature4' getappdata(handles.output,'FileName')],img); 
assignin('base',['SAEmodel4' getappdata(handles.output,'FileName')],sae); 
set(handles.statustext,'String','Finish SAE!');
% --- Executes on button press in SAElearnbutton.
function SAElearnbutton_Callback(hObject, eventdata, handles)
% hObject    handle to SAElearnbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
